﻿using System;
using System.Web;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.ACTP.Components;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class AddEditTaskMilestone : AddEditItemControl
    {
        private int _TeamID;
        public int TeamID
        {
            get
            {
                if (_TeamID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["TeamID"] != null)
                        _TeamID = Int32.Parse(Request.Params["TeamID"]);
                }
                return _TeamID;
            }
            set { _TeamID = value; }
        }

        private int _ProjectID;
        public int ProjectID
        {
            get
            {
                if (_ProjectID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["ProjectID"] != null)
                        _ProjectID = Int32.Parse(Request.Params["ProjectID"]);
                }
                return _ProjectID;
            }
            set { _ProjectID = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditTaskMilestone_Update;
            this.Delete += AddEditTaskMilestone_Delete;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // Projects of this team.
                ddlProject.DataSource = Project.SelectCollectionBy_TeamID(this.TeamID);
                ddlProject.DataBind();

                if (this.ItemID > 0)
                {
                    TaskMilestone taskmilestone = TaskMilestone.Load(this.ItemID);
                    if (taskmilestone != null)
                    {
                        ddlProject.SelectedValue = taskmilestone.ProjectID.ToString();
                        txtTitle.Text = taskmilestone.Title;
                        txtDescription.Text = taskmilestone.Description;
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }

        void AddEditTaskMilestone_Update(object sender, EventArgs e)
        {
            TaskMilestone taskmilestone = new TaskMilestone();
            if (this.ItemID > 0)
            {
                taskmilestone = TaskMilestone.Load(this.ItemID);
            }

            taskmilestone.ModuleID = this.ModuleID;
            taskmilestone.TeamID = this.TeamID;
            taskmilestone.ProjectID = Convert.ToInt32(ddlProject.SelectedValue);
            taskmilestone.Title = txtTitle.Text;
            taskmilestone.Description = txtDescription.Text;

            // Insert.
            if (this.ItemID == 0)
            {
                this.ItemID = taskmilestone.Insert();
            }
            // Update.
            else
            {
                taskmilestone.TaskMilestoneID = this.ItemID;
                taskmilestone.Update();
            }

            // Go back.
            this.ReturnToReferrerPage();

        }

        void AddEditTaskMilestone_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                TaskMilestone.DeleteTaskMilestone(this.ItemID);
                // Go back.
                this.ReturnToReferrerPage();
            }
        }
    }
}
